package com.pickgliss.ui
{
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.ui.core.SpriteLayer;
	import com.pickgliss.utils.DisplayUtils;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Rectangle;

	public class LayerManager
	{
		private var _stageTopLayer:SpriteLayer;
		private var _stageDynamicLayer:SpriteLayer;
		private var _stageBottomLayer:SpriteLayer;
		private var _gameTopLayer:SpriteLayer;
		private var _gameDynamicLayer:SpriteLayer;
		private var _gameUILayer:SpriteLayer;
		private var _gameBaseLayer:SpriteLayer;
		private var _gameBottomLayer:SpriteLayer;
		
		public static const STAGE_TOP_LAYER:int = 0;
		public static const STAGE_DYANMIC_LAYER:int = 1;
		public static const GAME_TOP_LAYER:int = 2;
		public static const GAME_DYNAMIC_LAYER:int = 3;
		public static const GAME_UI_LAYER:int = 4;
		public static const GAME_BASE_LAYER:int = 5;
		public static const GAME_BOTTOM_LAYER:int = 6;
		public static const STAGE_BOTTOM_LAYER:int = 7;
		
		/**
		 * 没有蒙版
		 */
		public static const NONE_BLOCKGOUND:int = 0;
		/**
		 * 黑色蒙版
		 */
		public static const BLCAK_BLOCKGOUND:int = 1;
		/**
		 * 白色蒙版
		 */
		public static const ALPHA_BLOCKGOUND:int = 2;
		public function LayerManager()
		{
		}
		
		private static var _instance:LayerManager;
		public static function get Instance():LayerManager
		{
			if(_instance == null)
			{
				_instance = new LayerManager();
			}
			return _instance;
		}
		
		public function setup(stage:Stage):void
		{
			_stageTopLayer = new SpriteLayer();
			_stageDynamicLayer = new SpriteLayer();
			_stageBottomLayer = new SpriteLayer(true);
			_gameTopLayer = new SpriteLayer();
			_gameDynamicLayer = new SpriteLayer();
			_gameUILayer = new SpriteLayer();
			_gameBaseLayer = new SpriteLayer();
			_gameBottomLayer = new SpriteLayer();
			
			stage.addChild(_stageBottomLayer);
			stage.addChild(_stageDynamicLayer);
			stage.addChild(_stageTopLayer);
			
			_gameDynamicLayer.autoClickTotop = true;
			
			_stageBottomLayer.addChild(_gameBottomLayer);
			_stageBottomLayer.addChild(_gameBaseLayer);
			_stageBottomLayer.addChild(_gameUILayer);
			_stageBottomLayer.addChild(_gameDynamicLayer);
			_stageBottomLayer.addChild(_gameTopLayer);
		}
		
		public function getLayerByType(type:int):SpriteLayer
		{
			switch(type)
			{
				case STAGE_TOP_LAYER:
					return _stageTopLayer;
					break;
				case STAGE_DYANMIC_LAYER:
					return _stageDynamicLayer;
					break;
				case GAME_TOP_LAYER:
					return _gameTopLayer;
					break;
				case GAME_DYNAMIC_LAYER:
					return _gameDynamicLayer;
					break;
				case GAME_BASE_LAYER:
					return _gameBaseLayer;
					break;
				case GAME_BOTTOM_LAYER:
					return _gameBottomLayer;
					break;
				case GAME_UI_LAYER:
					return _gameUILayer;
					break;
				case STAGE_BOTTOM_LAYER:
					return _stageBottomLayer;
					break;
			}
			return null;
		}
		
		public function addToLayer(source:DisplayObject,type:int,center:Boolean = false,blockBackgound:int = NONE_BLOCKGOUND,focusTop:Boolean = true):void
		{
			var container:SpriteLayer = getLayerByType(type);
			if(center)
			{
				if(source is Component)
				{
					source.x = (StageReferance.stageWidth - source.width)/2;
					source.y = (StageReferance.stageHeight - source.height)/2;
				}else
				{
					var visibleSize:Rectangle = DisplayUtils.getVisibleSize(source);
					source.x = (StageReferance.stageWidth - visibleSize.width)/2;
					source.y = (StageReferance.stageHeight - visibleSize.height)/2;
				}
			}
			
			container.addTolayer(source,blockBackgound,focusTop);
		}
		
		public function clearnStageDynamic():void
		{
			cleanSprite(_stageDynamicLayer);
		}
		
		public function clearnGameDynamic():void
		{
			cleanSprite(_gameDynamicLayer);
		}
		
		private function cleanSprite(target:Sprite):void
		{
			while(target.numChildren > 0)
			{
				var child:DisplayObject = target.getChildAt(0);
				ObjectUtils.disposeObject(child);
			}
		}
		
		/**
		 * 获得所有SpriteLayer的是否有蒙版在舞台上
		 */		
		public function get backGroundInParent():Boolean
		{
			if(!_stageTopLayer.backGroundInParent &&
			   !_stageDynamicLayer.backGroundInParent &&
			   !_stageBottomLayer.backGroundInParent &&
			   !_gameTopLayer.backGroundInParent &&
			   !_gameDynamicLayer.backGroundInParent &&
			   !_gameUILayer.backGroundInParent &&
			   !_gameBaseLayer.backGroundInParent &&
			   !_gameBottomLayer.backGroundInParent)
			{
				return false;
			}else
			{
				return true;
			}
		}
	}
}